
import {queryMobilePageConfig} from '@/api/system'
import {isJsonStr} from '@/utils/index'

export const useSystemTheme = (node: HTMLElement, params: any) => {

    function getTheme() {
        return new Promise<Array<string>>((resolve, reject) => {
            queryMobilePageConfig({
                ...params
            }).then((res) => {
                const { result:mobilePageConfigInfoVOS } = res.result;
                const {pageAttribute} = mobilePageConfigInfoVOS[0]
                if (isJsonStr(pageAttribute)) {
                    const _config = JSON.parse(pageAttribute)
                    const { colors } = _config
                    resolve(colors as Array<string>)
                }
            }).catch(err => {
                console.log(err)
                reject(err)
            })
        })
    }

    async function setTheme() {
        if (!node) {
            console.error('无效的参数');
            return;
        }
        const theme = await getTheme()
        theme.map((item, index) => {
            node.style.setProperty(`--color${index}`, item);
        })
    }

    return {
        setTheme
    }
}